package com.xxl.job.executor.repository;


import com.xxl.job.executor.common.RedisKeyConstant;
import com.xxl.job.executor.model.GatherDefModel;
import com.xxl.job.executor.model.GatherMjModel;
import com.xxl.job.executor.utils.JSONUtils;
import com.xxl.job.executor.utils.MapUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 查询采集口配置
 */
@Repository
public class GatherDefRepository {

    private static final Logger LOGGER = LoggerFactory.getLogger(GatherDefRepository.class);

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 获取采集口配置信息 单个
     * @param gatherId
     * @return
     */
    public GatherDefModel findGatherDefModel(String gatherId) {
        //redis  查询 字段
        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_DEF_GATHER);
        List<String> gatherDefModelJsonList = MapUtil.getLikeByMap(valueOperations.entries(),gatherId);
        if (gatherDefModelJsonList != null && gatherDefModelJsonList.size() > 0){
            for (String gatherDefModelStr : gatherDefModelJsonList) {

                //判断某字符串是否不为空且长度不为0且不由空白符(whitespace)构成    真正有东西 返回ture
                if (StringUtils.isNotBlank(gatherDefModelStr)) {
                    try {
                        //封装为json字符串
                        GatherDefModel gdm = JSONUtils.json2pojo(gatherDefModelStr, GatherDefModel.class);
                        if(gatherId.equals(gdm.getGatherId())){
                            return gdm;
                        }
                    } catch (IOException e) {
                        LOGGER.error("获取采集口配置信息终端JSON='{}' 转换异常！", gatherDefModelStr, e);
                    }
                }
            }
        }else {
            LOGGER.warn("没有 gatherid='{}' 对应的信号配置缓存！", gatherId);
            return null;
        }
        return null;
    }

    /**
     * 获取采集口配置信息 多个
     * @param gatherId
     * @return
     */
    public List<GatherDefModel> findGatherDefModelList(String gatherId){

        List<GatherDefModel> gatherDefModellList = new ArrayList<>();


        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_DEF_GATHER);
       //gather表中   key是这样设置的  3010001_30100010001
        List<String> gatherDefModelJsonList = MapUtil.getLikeByMap(valueOperations.entries(),gatherId+"_");


        if (gatherDefModelJsonList != null && gatherDefModelJsonList.size() > 0){
            for (String gatherDefModelStr : gatherDefModelJsonList) {
                if (StringUtils.isNotBlank(gatherDefModelStr)) {
                    try {
                        GatherDefModel gdm = JSONUtils.json2pojo(gatherDefModelStr, GatherDefModel.class);
                        if(gatherId.equals(gdm.getGatherId())){
                            gatherDefModellList.add(gdm);
                        }
                    } catch (IOException e) {
                        LOGGER.error("获取采集口配置信息终端JSON='{}' 转换异常！", gatherDefModelStr, e);
                    }
                }
            }
            return gatherDefModellList;
        }else {
            LOGGER.warn("没有 gatherid='{}' 对应的信号配置缓存！", gatherId);
            return null;
        }
    }

    /**
     * 查询所有IP不为空的启用采集口
     * @return
     */
    public List<GatherDefModel> findGatherDefModelAll(){

        List<GatherDefModel> gatherDefModellList = new ArrayList<>();
        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_DEF_GATHER);
        Map<String,String> gatherInfoAll = valueOperations.entries();
        for(Map.Entry<String,String> entry : gatherInfoAll.entrySet()){
            try {
                GatherDefModel gdm = JSONUtils.json2pojo(entry.getValue(), GatherDefModel.class);
                if("1".equals(gdm.getIsUse())&&gdm.getCollectIp()!=null&&gdm.getCollectIp().length()>0){
                    gatherDefModellList.add(gdm);
                }
            } catch (IOException e) {
                LOGGER.error("获取采集口配置信息终端JSON='{}' 转换异常！", entry.getValue(), e);
            }
        }
        return gatherDefModellList;
    }




    /**
     * 获取门禁配置信息 多个
     * @param gatherId
     * @return
     */
    public List<GatherMjModel> findGatherMjModelList(String gatherId){

        List<GatherMjModel> gatherMjModellList = new ArrayList<>();

        //通过  gatherId  获取redis中  习友路门禁信息
        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_DEF_GATHER);

        List<String>    JsonList = MapUtil.getLikeByMap(valueOperations.entries(),gatherId+"_");
        if (JsonList != null && JsonList.size() > 0){

            for (String gatherDefModelStr : JsonList) {

                if (StringUtils.isNotBlank(gatherDefModelStr)) {
                    try {
                        GatherMjModel gdm = JSONUtils.json2pojo(gatherDefModelStr, GatherMjModel.class);
                        //找到 gatherId相同的实体 存放在  gatherMjModellList 中
                        if(gatherId.equals(gdm.getGatherId())){
                            gatherMjModellList.add(gdm);
                        }
                    } catch (IOException e) {
                        LOGGER.error("获取门禁配置信息终端JSON='{}' 转换异常！", gatherDefModelStr, e);
                    }
                }
            }
            return gatherMjModellList;
        }else {
            LOGGER.warn("没有 gatherid='{}' 对应的信号配置缓存！", gatherId);
            return null;
        }
    }

}
